﻿using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using PassBag;
using PassBag.Logic.Crypt;
using PassBag.Logic;
using PassBag.Utils;

namespace TestPassBag {

    [TestFixture, Description ("Test encryption and decryption")]
    public class TestEncryption {
        private const int MIN_STRING_LENGTH = 0;
        private const int MAX_STRING_LENGTH = 10000;
        private const int LOOP_NUMBER = 1000;

        [Test, Description ("Test encryption with multisize password and string")]
        public void CheckCryptMultiSizeString () {
            string password = string.Empty;
            string stringToEncrypt = string.Empty;
            string encryptString = string.Empty;

            for (int i = 0; i < LOOP_NUMBER; i++) {
                password = Randomizer.GetRandomString (Randomizer.GetRandomNumber (5, 30), false);
                EncryptString crypt = new EncryptString (password);
                stringToEncrypt = Randomizer.GetRandomString (Randomizer.GetRandomNumber (MIN_STRING_LENGTH, MAX_STRING_LENGTH), false);

                encryptString = crypt.EncryptStringToHexString (stringToEncrypt);

                EncryptString decrypt = new EncryptString (password);

                Assert.AreEqual (stringToEncrypt, decrypt.DecryptHexStringToString (encryptString));
            }
        }

        [Test, Description ("Test zero length string")]
        public void ZeroLengthString() {
            string password = string.Empty;
            string stringToEncrypt = string.Empty;
            string encryptString = string.Empty;

            password = Randomizer.GetRandomString (Randomizer.GetRandomNumber (5, 10000), false);
            EncryptString crypt = new EncryptString (password);
            stringToEncrypt = string.Empty;

            encryptString = crypt.EncryptStringToHexString (stringToEncrypt);

            EncryptString decrypt = new EncryptString (password);

            Assert.AreEqual (stringToEncrypt, decrypt.DecryptHexStringToString (encryptString));
        }

        [Test, Description ("Test PasswordObject encrypt/decrypt")]
        public void PasswordObjectEncryptDecrypt () {

            for (int i = 0; i < 100; i++) {
                string password = Randomizer.GetRandomString (Randomizer.GetRandomNumber (5, 30), false);
                PasswordObject source = new PasswordObject (Guid.NewGuid ());

                source.Information = Randomizer.GetRandomString (Randomizer.GetRandomNumber (MIN_STRING_LENGTH, MAX_STRING_LENGTH), false);
                source.Title = Randomizer.GetRandomString (Randomizer.GetRandomNumber (MIN_STRING_LENGTH, MAX_STRING_LENGTH), false);
                source.UserName = Randomizer.GetRandomString (Randomizer.GetRandomNumber (MIN_STRING_LENGTH, MAX_STRING_LENGTH), false);
                source.Password = Randomizer.GetRandomString (Randomizer.GetRandomNumber (MIN_STRING_LENGTH, MAX_STRING_LENGTH), false);
                source.Url = Randomizer.GetRandomString (Randomizer.GetRandomNumber (MIN_STRING_LENGTH, MAX_STRING_LENGTH), false);
                source.Comment = Randomizer.GetRandomString (Randomizer.GetRandomNumber (MIN_STRING_LENGTH, MAX_STRING_LENGTH), false);
                source.LastModified = DateTime.Parse (Randomizer.GetRandomDateTime ().ToString ());

                PasswordCryptObject destination = EncryptPasswordObject.Encrypt (source, password);
                PasswordObject descryptObject = EncryptPasswordObject.Decrypt (destination, password);

                Assert.AreEqual (source.Id, descryptObject.Id);
                Assert.AreEqual (source.Information, descryptObject.Information);
                Assert.AreEqual (source.Title, descryptObject.Title);
                Assert.AreEqual (source.UserName, descryptObject.UserName);
                Assert.AreEqual (source.Password, descryptObject.Password);
                Assert.AreEqual (source.Url, descryptObject.Url);
                Assert.AreEqual (source.Comment, descryptObject.Comment);
                Assert.AreEqual (source.LastModified, descryptObject.LastModified);
                
            }            
        }

    }
}
